﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace Windy.Transformations.Test.Samples
{
    public class AlgaeLSystem
    {
        /*
        http://en.wikipedia.org/wiki/L-system#Example_1:_Algae
        Example 1: Algae
        Lindenmayer's original L-system for modelling the growth of algae.
        variables : A B
        constants : none
        start  : A
        rules  : (A → AB), (B → A)
        which produces:
        n = 0 : A
        n = 1 : AB
        n = 2 : ABA
        n = 3 : ABAAB
        n = 4 : ABAABABA
        n = 5 : ABAABABAABAAB
        n = 6 : ABAABABAABAABABAABABA
        n = 7 : ABAABABAABAABABAABABAABAABABAABAAB
        */

        [Fact()]
        public void AlgaeTest()
        {
            var algaeRuleSet = new Transformation<char, char, char>(
                            (c) => c,
                            new ExpandTransformRule<char, char, char>('A', () => "AB"),
                            new ReplaceTransformRule<char, char, char>('B', () => 'A'),
                            new BlockTransformRule<char>() // Works as a protector that eliminate the unregistered items 
                            );

            string[] statusList = new string[]
            {
                "A",
                "AB",
                "ABA",
                "ABAAB",
                "ABAABABA",
                "ABAABABAABAAB",
                "ABAABABAABAABABAABABA",
                "ABAABABAABAABABAABABAABAABABAABAAB",
            };

            var current = new List<char>(statusList[0]);

            for (int fo = 1; fo < statusList.Length; fo++)
            {
                algaeRuleSet.ApplyToList(current);
                var currentString = new string(current.ToArray());

                Assert.Equal(statusList[fo], currentString);
            }
        }
    }
}
